package com.zsyh.jiujiu.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.SPUtils;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MyInfoBeeean;
import com.zsyh.jiujiu.bean.SucessBean;
import com.zsyh.jiujiu.dialog.ClallBackData;
import com.zsyh.jiujiu.dialog.SignDialog;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.GlideCircleTransform;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MySignActivity extends BaseActivity {

    @BindView(R.id.re_back)
    RelativeLayout reBack;
    @BindView(R.id.one)
    TextView one;
    @BindView(R.id.two)
    TextView two;
    @BindView(R.id.three)
    TextView three;
    @BindView(R.id.lintv)
    LinearLayout lintv;
    @BindView(R.id.signnumber)
    TextView signnumber;
    @BindView(R.id.tvone)
    TextView tvone;
    @BindView(R.id.tvttwo)
    TextView tvttwo;
    @BindView(R.id.recen)
    RelativeLayout recen;
    @BindView(R.id.bntsign)
    TextView bntsign;



    private String issign;
    private String ssignnum;

    @Override
    protected int initLayout() {
        return R.layout.activity_sign;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        gettUser((String) SPUtils.get(thisActivity, "userid", ""));
    }

    @Override
    protected void initEvent() {

    }




    @OnClick({R.id.re_back, R.id.bntsign})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.re_back:
                thisActivity.finish();
                break;
            case R.id.bntsign:
                if(issign.equals("1")){

                }else if(issign.equals("0")){
                    Sign((String) SPUtils.get(thisActivity, "userid", ""));
                }

                break;
        }
    }

    /**
     * 签到
     */
    public void Sign(String userid){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .userSign(userid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Gson gson=new Gson();
                        SucessBean sucessBean=gson.fromJson(JsonStr,SucessBean.class);
                        //three.setText(Integer.valueOf( three.getText().toString())+1+"");
                        SignDialog signDialog=new SignDialog(Integer.valueOf(sucessBean.getData())+"");
                        signDialog.setSelectAddresFinish(new ClallBackData() {
                            @Override
                            public void getData(String data, String name, String number) {
                                thisActivity.finish();
                            }
                        });
                        signDialog.show(getSupportFragmentManager(),"");
                        bntsign.setBackgroundResource(R.mipmap.ic_sign_hui);

                    }
                });

    }


    /**
     * getuser
     */
    public void gettUser(String userid) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getUserById(userid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(thisActivity, false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("Myinfo", JsonStr + "");
                        Gson gson = new Gson();
                        MyInfoBeeean myInfoBeeean = gson.fromJson(JsonStr, MyInfoBeeean.class);
                        issign=myInfoBeeean.getData().getIsSign();
                        if(issign.equals("1")){
                            bntsign.setBackgroundResource(R.mipmap.ic_sign_hui);
                        }else if(issign.equals("0")){
                            bntsign.setBackgroundResource(R.mipmap.ic_bntdi);
                        }
                        String num=myInfoBeeean.getData().getSignnum()+"";
                        ssignnum=num;
                        signnumber.setText(num);
                        if(num.length()==1){
                            one.setText("0");
                            two.setText("0");
                            three.setText(num);
                        }else if(num.length()==2){
                            one.setText("0");
                            two.setText(num.substring(0,1));
                            three.setText(num.substring(num.length()-1,num.length()));
                        }else if(num.length()==3){
                            one.setText(num.substring(0,1));
                            two.setText(num.substring(1,2));
                            three.setText(num.substring(num.length()-1,num.length()));
                        }

                    }
                });
    }

}
